home *** CD-ROM | disk | FTP | other *** search
/ MacSilverWare / macsilverware.iso / Screen Savers / DarkSide 4.0 / FaderShell / Fader.c next >
Text File  |  1993-06-01  |  9KB  |  337 lines

  1. /*
  2.     DarkSide 4.0 - a 7.0 dependant, system clean expandable screen saver.
  3.     
  4.     copyright © 1990, 1991, 1992, 1993 by Tom Dowdy
  5.     All rights reserved.
  6.     
  7.     This fader shell serves to dispatch requests from the main DarkSide code
  8.     into the appropriate entry points.
  9. */
  10. #include <StdArg.h>
  11. #include <Memory.h>
  12. #include <Packages.h>
  13. #include <Errors.h>
  14.  
  15. #include "Fader.h"
  16.  
  17.  
  18. /* ------------------------------------------------------------------------    */
  19. /* GLOBAL HANDLING ROUTINES */
  20. /* ------------------------------------------------------------------------    */
  21. OSErr    CreateA5World(Ptr * a5World);
  22. void    DisposeA5World(Ptr a5World, Ptr appA5);
  23. void    DebugLongInt(long aLong);
  24.  
  25. /* ------------------------------------------------------------------------    */
  26. OSErr    FaderEntry(long selector, Ptr *a5World, MachineInfoPtr machineInfo, ...)
  27. {
  28.     OSErr    anErr;
  29.     va_list    nextArg;
  30.     
  31.  
  32.         
  33.     // start stripping optional arguments
  34.     va_start(nextArg, machineInfo);
  35.     
  36.     switch(selector)
  37.         {
  38.         case preflightFader:
  39.             {
  40.             long    *minTicks, *maxTicks;
  41.             
  42.             minTicks = va_arg(nextArg, long*);
  43.             maxTicks = va_arg(nextArg, long*);
  44.             
  45.             anErr = CreateA5World(a5World);
  46.             if (anErr == noErr)
  47.                 {
  48.                 (void) SetA5((long) *a5World);
  49.                 BlockMove(machineInfo->applicationQD, &qd, sizeof(qd));
  50.                 anErr = PreflightFader(machineInfo, minTicks, maxTicks);
  51.                 BlockMove(&qd, machineInfo->applicationQD, sizeof(qd));
  52.                 }
  53.             }
  54.             break;
  55.             
  56.         case initializeFader:
  57.             (void) SetA5((long) *a5World);
  58.             BlockMove(machineInfo->applicationQD, &qd, sizeof(qd));
  59.             anErr = InitializeFader(machineInfo);
  60.             BlockMove(&qd, machineInfo->applicationQD, sizeof(qd));
  61.             break;
  62.             
  63.         case idleFader:
  64.             (void) SetA5((long) *a5World);
  65.             anErr = IdleFader(machineInfo);
  66.             break;
  67.             
  68.         case disposeFader:
  69.             {
  70.             
  71.             (void) SetA5((long) *a5World);
  72.             BlockMove(machineInfo->applicationQD, &qd, sizeof(qd));
  73.             anErr = DisposeFader(machineInfo);
  74.             BlockMove(&qd, machineInfo->applicationQD, sizeof(qd));
  75.             DisposeA5World(*a5World, (Ptr)machineInfo->applicationA5);
  76.             
  77.             }
  78.             break;
  79.  
  80.         case updateFader:
  81.             (void) SetA5((long) *a5World);
  82.             anErr = UpdateFader(machineInfo);
  83.             break;
  84.             
  85.         case hitFader:
  86.             {
  87.             DialogPtr    dPtr;
  88.             long        itemHit;
  89.             long        itemOffset;
  90.                         
  91.             dPtr         = va_arg(nextArg, DialogPtr);
  92.             itemHit     = va_arg(nextArg, long);
  93.             itemOffset     = va_arg(nextArg, long);
  94.  
  95.             anErr = HitFader(machineInfo, dPtr, itemHit, itemOffset);            
  96.             }
  97.             
  98.             break;
  99.             
  100.         default:
  101.             // function not found error
  102.             anErr = fnfErr;
  103.             break;
  104.         }
  105.         
  106.     va_end(nextArg);
  107.     (void) SetA5(machineInfo->applicationA5);
  108.     return(anErr);    
  109.     
  110. } // FaderEntry
  111.  
  112. /* ------------------------------------------------------------------------    */
  113. /* DEBUGGING ROUTINES                             */
  114. /* ------------------------------------------------------------------------    */
  115. void DebugLongInt(long theLong)
  116. {
  117.     Str255 theString;
  118.     
  119.     NumToString(theLong, theString);
  120.     DebugStr(theString);
  121.     
  122. } // DebugLongInt
  123.  
  124. /* ------------------------------------------------------------------------    */
  125. /* FUN A5 STUFF - See Tech note 256 for details                             */
  126. /* ------------------------------------------------------------------------    */
  127. long A5Size(); 
  128. void A5Init(Ptr theA5);
  129.  
  130. OSErr    CreateA5World(Ptr * a5World)
  131. {
  132.     OSErr    anErr;
  133.     Ptr        theWorld = nil;
  134.     Handle    worldHandle;
  135.     
  136.     worldHandle = BestNewHandle(A5Size());
  137.     anErr = MemError();
  138.     if (anErr == noErr)
  139.         {
  140.         MoveHHi(worldHandle);
  141.         HLock(worldHandle);
  142.         theWorld = *worldHandle;
  143.         
  144.         theWorld += + A5Size() - 32;
  145.         A5Init(theWorld);
  146.         
  147.         // very important if anyone wants to call SwapMMUMode
  148.         theWorld = StripAddress(theWorld);
  149.         }
  150.     *a5World = theWorld;
  151.     
  152.     return(anErr);
  153.     
  154. } // CreateA5World
  155.  
  156. /* ------------------------------------------------------------------------    */
  157. void    DisposeA5World(Ptr a5World, Ptr appA5)
  158. {
  159.     Handle    worldHandle;
  160.     
  161.     (void) SetA5((long) appA5);
  162.     
  163.     worldHandle = RecoverHandle(a5World - A5Size() + 32);
  164.     DisposHandle(worldHandle);
  165.     
  166. } // DisposeA5World
  167.  
  168. /* ------------------------------------------------------------------------    */
  169. /* FADER UTILS                                                                 */
  170. /* ------------------------------------------------------------------------    */
  171.  
  172. Handle    BestNewHandle(Size    theSize)
  173. /*
  174.     Tries to get the handle from the temp memory first, if that fails, it goes
  175.     to the application.
  176. */
  177. {
  178.     Handle theHandle;
  179.     OSErr    anErr;
  180.     
  181.     theHandle = TempNewHandle(theSize, &anErr);
  182.     if (theHandle == nil)
  183.         theHandle = NewHandle(theSize);
  184.         
  185.     return(theHandle);
  186.     
  187. } // BestNewHandle
  188.  
  189. /* ------------------------------------------------------------------------    */
  190.  
  191. RgnHandle    BestNewRgn()
  192. /*
  193.     Tries to get a rgn handle from the temp memory first, if that fails, it goes
  194.     to the application.  Needs enough room in the app heap to create the region
  195.     in the first place.
  196. */
  197. {
  198.     RgnHandle     theRgn;
  199.     OSErr        anErr;
  200.     
  201.     // make a region
  202.     theRgn = NewRgn();
  203.     if (theRgn != nil)
  204.         {
  205.         RgnHandle    theHandle;
  206.         short        regionSize;
  207.         
  208.         // try to make something the same size in the temp memory
  209.         regionSize = GetHandleSize((Handle) theRgn);
  210.         theHandle = (RgnHandle) TempNewHandle(regionSize, &anErr);
  211.         if (anErr == noErr)
  212.             {
  213.             // if we get it, use that one for our region
  214.             BlockMove(*theRgn, *theHandle, regionSize);
  215.             DisposeRgn(theRgn);
  216.             theRgn = theHandle;
  217.             }
  218.         }
  219.         
  220.     return(theRgn);
  221.     
  222. } // BestNewRgn
  223.  
  224.  
  225.  
  226. /* ------------------------------------------------------------------------    */
  227.  
  228. short    Rnd(long max)
  229. /*
  230.     Returns a number > 0 and < max
  231. */
  232. {
  233. unsigned long value;
  234.  
  235.     value = (unsigned short)max * (unsigned short)Random();
  236.     value >>= 16;
  237.     return(value);
  238.     
  239. } // Rnd
  240.  
  241. /* ------------------------------------------------------------------------    */
  242. void PlaceRectOnScreen(
  243.     MachineInfoPtr machineInfo,    // give info about the machine here
  244.     short width,                // width of rect, can be 0
  245.     short height,                // height of rect, can be 0
  246.     Rect * placedRect,            // Placed rect is returned here
  247.     Rect * margins,                // margins around screen, can be nil
  248.     short * whichScreen)        // screen index returned here, can be nil
  249. {
  250.     Rect        screenRect;
  251.     short        pickScreen;
  252.  
  253.     // pick a random screen    
  254.     pickScreen = Rnd(machineInfo->numScreens);
  255.     screenRect = machineInfo->theScreens[pickScreen].bounds;
  256.     if (whichScreen != nil)
  257.         *whichScreen = pickScreen;
  258.         
  259.     if (margins != nil)
  260.         {
  261.         screenRect.top += margins->top;
  262.         screenRect.left += margins->left;
  263.         screenRect.bottom -= margins->bottom;
  264.         screenRect.right -= margins->right;
  265.         }
  266.         
  267.     screenRect.right -= width;
  268.     screenRect.bottom -= height;
  269.     
  270.     if (placedRect != nil)
  271.         {
  272.         placedRect->top = screenRect.top + Rnd(screenRect.bottom - screenRect.top);
  273.         placedRect->left = screenRect.left + Rnd(screenRect.right - screenRect.left);
  274.         placedRect->bottom = placedRect->top + height;
  275.         placedRect->right = placedRect->left + width;
  276.         }
  277.         
  278. } // PlaceRectOnScreen
  279.  
  280. /* ------------------------------------------------------------------------    */
  281. /* CALLBACK DEFINES
  282. /* ------------------------------------------------------------------------    */
  283. #pragma parameter __D0 DoWritePreferencesHandle(__A0)
  284. OSErr DoWritePreferencesHandle(Ptr M, Handle h, ResType theType) = 
  285.     { 0x7000, 0x487B, 0x000A, 0x2F3C, 0x0800, 0x0004, 0x4ED0};
  286. #pragma parameter __D0 DoReadPreferencesHandle(__A0)
  287. OSErr DoReadPreferencesHandle(Ptr M, Handle *h, ResType theType) = 
  288.     { 0x7000, 0x487B, 0x000A, 0x2F3C, 0x0800, 0x0008, 0x4ED0};
  289. #pragma parameter __D0 DoPlayResourceSnd(__A0)
  290. OSErr DoPlayResourceSnd(Ptr M, short theID, Boolean async) = 
  291.     { 0x7000, 0x487B, 0x000A, 0x2F3C, 0x0800, 0x000C, 0x4ED0};
  292.  
  293. /* ------------------------------------------------------------------------    */
  294. /* CALLBACK WRAPERS
  295. /* ------------------------------------------------------------------------    */
  296. OSErr    WritePreferencesHandle(MachineInfoPtr machineInfo, Handle h, ResType theType)
  297. {
  298.     OSErr    anErr;
  299.     long    curA5;
  300.     
  301.     curA5 = SetA5(machineInfo->applicationA5);
  302.     anErr = DoWritePreferencesHandle(machineInfo->callbackLoader, h, theType);
  303.     
  304.     SetA5(curA5);
  305.     return(anErr);
  306.     
  307. } // WritePreferencesHandle
  308.  
  309. /* ------------------------------------------------------------------------    */
  310. OSErr    ReadPreferencesHandle(MachineInfoPtr machineInfo, Handle *h, ResType theType)
  311. {
  312.     OSErr    anErr;
  313.     long    curA5;
  314.     
  315.     curA5 = SetA5(machineInfo->applicationA5);
  316.     anErr = DoReadPreferencesHandle(machineInfo->callbackLoader, h, theType);
  317.     
  318.     SetA5(curA5);
  319.     return(anErr);
  320.     
  321. } // ReadPreferencesHandle
  322.  
  323. /* ------------------------------------------------------------------------    */
  324. OSErr    PlayResourceSnd(MachineInfoPtr machineInfo, short theID, Boolean async)
  325. {
  326.     OSErr    anErr;
  327.     long    curA5;
  328.     
  329.     curA5 = SetA5(machineInfo->applicationA5);
  330.     anErr = DoPlayResourceSnd(machineInfo->callbackLoader, theID, async);
  331.     
  332.     SetA5(curA5);
  333.     return(anErr);
  334.     
  335. } // PlayResourceSnd
  336.  
  337.